package com.wuwei.elearning.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.elearning.enumPackage.Constant;
import com.wuwei.elearning.exception.ElearningMysqlException;
import com.wuwei.elearning.question.domain.Audio;
import com.wuwei.elearning.question.domain.AudioQuestion;
import com.wuwei.elearning.question.mapper.AudioMapper;
import com.wuwei.elearning.question.mapper.AudioQuestionMapper;
import com.wuwei.elearning.question.service.AudioService;
import com.wuwei.elearning.service.CacheService;
import com.wuwei.elearning.utils.MinioUtil;
import io.minio.GetObjectResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import com.wuwei.elearning.config.MinioConfig;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Service
public class AudioServiceImpl extends ServiceImpl<AudioMapper, Audio> implements AudioService {

    @Resource
    private AudioMapper audioMapper;

    @Resource
    private MinioUtil minioUtil;

    @Override
    @Transactional
    public void insert(MultipartFile file, Long account, String duration) {
        String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        LambdaQueryWrapper<Audio> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Audio::getName, fileName);
        Audio audio = audioMapper.selectOne(queryWrapper);
        if (audio != null) {
            throw new ElearningMysqlException(30011, "上传失败！该文件已存在！");
        } else {
            String path = null;
            try {
                path = minioUtil.upload(file, Constant.AUDIO_DIR);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            audio = new Audio();
            audio.setDuration(duration);
            audio.setCreatedBy(account);
            audio.setCreatedTime(LocalDateTime.now());
            audio.setName(fileName);
            audio.setPath(path);
            audioMapper.insert(audio);
            String upload = Constant.AUDIO_DIR + "/" + file.getOriginalFilename();
            String previewUrl = minioConfig.getEndpoint() + minioUtil.getPreviewUrl(upload);
            String redisKey = Constant.MINIO_AUDIO + path;
            cacheService.setEx(redisKey, previewUrl, 6, TimeUnit.DAYS);
        }
    }

    @Resource
    private CacheService cacheService;

    @Resource
    private MinioConfig minioConfig;

    /**
     * 删除数据库中数据
     * 删除硬盘中数据
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        Audio audio = audioMapper.selectById(id);
        String path = audio.getPath();
        minioUtil.remove(path);
        String redisKey = Constant.MINIO_AUDIO + path;
        cacheService.delete(redisKey);
        audioMapper.deleteById(id);
    }

    @Resource
    private AudioQuestionMapper audioQuestionMapper;

    /**
     * 更新audio表
     * 更新audioQuestion映射表
     *
     * @param audio
     */
    @Override
    @Transactional
    public void myUpdateById(Audio audio) {
        audioMapper.updateById(audio);
        String answers = audio.getAnswers();
        String questions = audio.getQuestions();
        String name = audio.getName();
        LambdaQueryWrapper<AudioQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AudioQuestion::getFileName, name);
        AudioQuestion audioQuestion = audioQuestionMapper.selectOne(wrapper);
        if (audioQuestion != null) {
            audioQuestion.setQuestions(questions);
            audioQuestion.setAnswers(answers);
            audioQuestion.setFileName(name);
            audioQuestionMapper.update(audioQuestion, wrapper);
        }
    }

}
